package com.jie.algorithm.ui.main;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jie.algorithm.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainVH> {

    private List<MainBean> mList;

    public MainAdapter(List<MainBean> list){
        this.mList = list;
    }

    @NonNull
    @Override
    public MainAdapter.MainVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(R.layout.adapter_main, viewGroup, false);
        return new MainVH(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MainAdapter.MainVH viewHolder, int i) {
        MainBean mainBean = mList.get(i);
        View view = viewHolder.findView(R.id.view);
        int height = mainBean.getHeight();
        int width = mainBean.getWidth();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
        view.setLayoutParams(params);
        view.setBackgroundColor(mainBean.getColor());
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public static class MainVH extends RecyclerView.ViewHolder{

        public MainVH(@NonNull View itemView) {
            super(itemView);
        }

        View findView(int id){
            return itemView.findViewById(id);
        }
    }
}
